<template>
  <div class="container">
    <el-dialog title="添加" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form
        :model="dialogFrom"
        :rules="rules"
        ref="addFrom"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="dialogFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="所教课程" prop="course">
          <el-input v-model="dialogFrom.course"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model.number="dialogFrom.price"></el-input>
        </el-form-item>
        <el-form-item label="销量" prop="num">
          <el-input v-model.number="dialogFrom.num"></el-input>
        </el-form-item>
        <el-form-item label="上传人" prop="person">
          <el-input v-model="dialogFrom.person"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeParentFun">取 消</el-button>
        <el-button type="primary" @click="submitForm('addFrom')">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addfunApi, updatefunApi } from "../../../api/teacherApi";

export default {
  props: ["dialogRow", "btnType"],
  data() {
    return {
      dialogVisible: true,
      dialogFrom: {
        name: "",
        course: "",
        price: "", //价格
        num: "", //销量
        person: "", //上传人
      },
      rules: {
        name: [
          { required: true, message: "请输入教师姓名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        course: [
          { required: true, message: "请输入所教课程", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        num: [{ required: true, message: "请输入销量", trigger: "blur" }],
        person: [{ required: true, message: "请输入上传人", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.dialogFrom = JSON.parse(JSON.stringify(this.dialogRow));
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.btnType == 0) {
            //添加逻辑
            // alert('submit!');
            //添加第二步
            //打包数据 发送请求  后端把数据加入数组 通知前端 前端弹框 刷新表格
            addfunApi(this.dialogFrom).then((res) => {
              console.log(res); //关闭弹框 刷新表格
              if (res.code == 200) {
                this.$parent.isShow = false; //关闭弹框
                this.$emit("resettablefun"); //刷新表格
              }
            });
          } else {
            //修改逻辑
            //打包数据，发送请求 后台数据修改 通知前端 关闭弹框 刷新表格
            updatefunApi(this.dialogFrom).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$parent.isShow = false; //关闭弹框
                this.$emit("resettablefun"); //刷新表格
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    closeParentFun() {
      this.$parent.isShow = false;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.$parent.isShow = false;
        })
        .catch((_) => {});
    },
  },
};
</script>

<style  lang="less" scoped>
</style>